//
//  FormalTabBar.m
//  LoanOne
//
//  Created by WangZhenyu on 2017/10/5.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "FormalTabBar.h"
#import "SharedViewControllers.h"
#import "FormalOneViewController.h"
#import "FormalTwoViewController.h"
#import "FormalThreeViewController.h"
#import "FormalFourViewController.h"
#import "AppDelegate.h"

#define BTN_X_OFF               0                           //按钮离顶部以及底部的距离
#define BTN_Y_OFF               0                           //两端按钮离边框的距离
#define BTN_WIDTH               SCREEN_WIDTH_DEVICE/4.0     //按钮宽度
#define BTN_HEIGHT              50                          //按钮高度

@interface FormalTabBar()

@property (strong, nonatomic) UIButton                  *btnOne;
@property (strong, nonatomic) UIButton                  *btnTwo;
@property (strong, nonatomic) UIButton                  *btnThree;
@property (strong, nonatomic) UIButton                  *btnFour;

@end


@implementation FormalTabBar


+ (FormalTabBar *)sharedFormalTabBar{
    static FormalTabBar *_sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[FormalTabBar alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE - 50 - IS_iPhoneX_Bottom, SCREEN_WIDTH_DEVICE, 50 + IS_iPhoneX_Bottom)];
        
    });
    return _sharedInstance;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        // Initialization code
        self.backgroundColor = [UIColor whiteColor];
        //self.backgroundColor = [UIColor redColor];
        //添加底部线条
        UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, 0.5)];
        [lineImageView setBackgroundColor:RGBACOLOR(179, 179, 179, 1)];
        [self addSubview:lineImageView];
        
        if (!self.btnOne) {
            self.btnOne = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.btnOne addTarget:self action:@selector(doBtnOne:) forControlEvents:UIControlEventTouchUpInside];
            self.btnOne.frame = CGRectMake(BTN_X_OFF, BTN_Y_OFF, BTN_WIDTH, BTN_HEIGHT);
            [self.btnOne setImage:[UIImage imageNamed:@"shouye_n_btn"] forState:UIControlStateNormal];
            [self.btnOne setImage:[UIImage imageNamed:@"shouye_s_btn"] forState:UIControlStateHighlighted];
            [self.btnOne setImage:[UIImage imageNamed:@"shouye_s_btn"] forState:UIControlStateSelected];
            [self addSubview:self.btnOne];
            
        }
        if (!self.btnTwo) {
            self.btnTwo = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.btnTwo addTarget:self action:@selector(doBtnTwo:) forControlEvents:UIControlEventTouchUpInside];
            self.btnTwo.frame = CGRectMake(BTN_WIDTH, BTN_Y_OFF, BTN_WIDTH, BTN_HEIGHT);
            [self.btnTwo setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.btnTwo setImage:[UIImage imageNamed:@"daikuan_n_btn"] forState:UIControlStateNormal];
            [self.btnTwo setImage:[UIImage imageNamed:@"daikuan_s_btn"] forState:UIControlStateHighlighted];
            [self.btnTwo setImage:[UIImage imageNamed:@"daikuan_s_btn"] forState:UIControlStateSelected];
            [self addSubview:self.btnTwo];
        }
        if (!self.btnThree) {
            self.btnThree = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.btnThree addTarget:self action:@selector(doBtnThree:) forControlEvents:UIControlEventTouchUpInside];
            self.btnThree.frame = CGRectMake(BTN_WIDTH * 2, BTN_Y_OFF, BTN_WIDTH, BTN_HEIGHT);
            [self.btnThree setImage:[UIImage imageNamed:@"zhushou_n_btn"] forState:UIControlStateNormal];
            [self.btnThree setImage:[UIImage imageNamed:@"zhushou_s_btn"] forState:UIControlStateHighlighted];
            [self.btnThree setImage:[UIImage imageNamed:@"zhushou_s_btn"] forState:UIControlStateSelected];
            [self addSubview:self.btnThree];
        }
        if (!self.btnFour) {
            self.btnFour = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.btnFour addTarget:self action:@selector(doBtnFour:) forControlEvents:UIControlEventTouchUpInside];
            self.btnFour.frame = CGRectMake(BTN_WIDTH * 3, BTN_Y_OFF, BTN_WIDTH, BTN_HEIGHT);
            [self.btnFour setImage:[UIImage imageNamed:@"wode_n_btn"] forState:UIControlStateNormal];
            [self.btnFour setImage:[UIImage imageNamed:@"wode_s_btn"] forState:UIControlStateHighlighted];
            [self.btnFour setImage:[UIImage imageNamed:@"wode_s_btn"] forState:UIControlStateSelected];
            [self addSubview:self.btnFour];
        }
    }
    return self;
}

- (void)setCurrentViewControllerIndex:(NSInteger)index{
    [self.btnOne setSelected:NO];
    [self.btnTwo setSelected:NO];
    [self.btnThree setSelected:NO];
    [self.btnFour setSelected:NO];
    switch (index) {
        case 1:
            [self.btnOne setSelected:YES];
            break;
        case 2:
            [self.btnTwo setSelected:YES];
            break;
        case 3:
            [self.btnThree setSelected:YES];
            break;
        case 4:
            [self.btnFour setSelected:YES];
            break;
        default:
            break;
    }
}

- (void)doBtnOne:(id)sender {
    if (!self.btnOne.selected) {
        FormalOneViewController *kViewCon = [SharedViewControllers formalOneViewCon];
        AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        if ([del.navigationController.viewControllers containsObject:kViewCon]) {
            [del.navigationController popToViewController:kViewCon animated:NO];
        }else {
            [del.navigationController pushViewController:kViewCon animated:NO];
        }
    }
}

- (void)doBtnTwo:(id)sender {
    if (!self.btnTwo.selected) {
        FormalTwoViewController *kViewCon = [SharedViewControllers formalTwoViewCon];
        AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        if ([del.navigationController.viewControllers containsObject:kViewCon]) {
            [del.navigationController popToViewController:kViewCon animated:NO];
        }else {
            [del.navigationController pushViewController:kViewCon animated:NO];
        }
        
    }
}

- (void)doBtnThree:(id)sender {
    if (!self.btnThree.selected) {
        FormalThreeViewController *kViewCon = [SharedViewControllers formalThreeViewCon];
        AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        if ([del.navigationController.viewControllers containsObject:kViewCon]) {
            [del.navigationController popToViewController:kViewCon animated:NO];
        }else {
            [del.navigationController pushViewController:kViewCon animated:NO];
        }
    }
}

- (void)doBtnFour:(id)sender {
    if (!self.btnFour.selected) {
        FormalFourViewController *kViewCon = [SharedViewControllers formalFourViewCon];
        AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        if ([del.navigationController.viewControllers containsObject:kViewCon]) {
            [del.navigationController popToViewController:kViewCon animated:NO];
        }else {
            [del.navigationController pushViewController:kViewCon animated:NO];
        }
    }
}

@end
